<?php
class cmsdream_pager {
	protected $_total = 0;
	protected $_totalPage = 1;
	protected $_currentPage = 1;
	protected $_pageSize = 10;
	protected $_pageTpl;
	protected $_pageHolder = "%page%";
	
	protected $_splitPage = 3;
	protected $_splitWord = '...';
	
	public $prevWords = array('text'=>'Previous','title'=>'Previous');
	public $nextWords = array('text'=>'Next','title'=>'Next');
	
	public function render() {
		if($GLOBALS['Q']->is_wap && $GLOBALS['Q']->client == 'wap') {
			$s = $this->get_render();
			$s = preg_replace('/<li([^>]*?)>/i', '', $s);
			$s = preg_replace('/<\/li>/i', ' ', $s);
			$s = preg_replace('/<a\s+href="[^"]+"\s+class="current">([\d]+)<\/a>/i', '<b>$1</b>', $s);
			echo $s;
		} else {
			echo $this->get_render();
		}
	}

	private function get_render(){
		if ($this->_total < 1) {
			return '';
		}
		$s = '';
		$from = max(1, $this->_currentPage - $this->_splitPage);
		$to = min($this->_totalPage, $this->_currentPage + $this->_splitPage);

		if ($this->_currentPage > 1) {
			$s .= '<li><a title="' . $this->prevWords['title'] . '" href="' 
				. str_replace($this->_pageHolder, $this->_currentPage - 1, $this->_pageTpl) . '">'
				. $this->prevWords['text'] . '</a></li>';
		}

		if ($from > 1) {
			$s .= '<li><a href="' . str_replace($this->_pageHolder, 1, $this->_pageTpl) . '">1</a></li>';

			if ($from > 2) {
				$s .= '<li>' . $this->_splitWord . '</li>';
			}
		}

		for ($i = $from; $i <= $to; $i ++) {
			$s .= '<li' . ($i != $this->_currentPage ? '' : ' class="current"') . '><a href="' .
				str_replace($this->_pageHolder, $i, $this->_pageTpl) . '"' . 
				($i != $this->_currentPage ? '' : ' class="current"') . '>' . $i . '</a></li>';
		}

		if ($to < $this->_totalPage) {
			if ($to < $this->_totalPage - 1) {
				$s .= '<li>' . $this->_splitWord . '</li>';
			}
			$s .= '<li><a href="' . str_replace($this->_pageHolder, $this->_totalPage, $this->_pageTpl) . '">' . $this->_totalPage . '</a></li>';
		}

		if ($this->_currentPage < $this->_totalPage) {
			$s .= '<li><a title="' . $this->nextWords['title'] . '" href="' 
				. str_replace($this->_pageHolder, $this->_currentPage + 1, $this->_pageTpl) . '">' 
				. $this->nextWords['text'] . '</a></li>';
			}
			return $s;
		}
	
	public function setTotal($total = 0) {
		$this->_total = intval($total) < 0 ? 0 : intval($total);
		$this->_totalPage = ceil($this->_total / $this->_pageSize);
	}
	
	public function getTotal() {
		return $this->_total;
	}
	
	public function setPagesize($pageSize) {
		$this->_pageSize = intval($pageSize) < 1 ? 1 : intval($pageSize);
		$this->_totalPage = ceil($this->_total / $this->_pageSize);
	}
	
	public function getPagesize() {
		return $this->_pageSize;
	}
	
	public function getCurrentpage() {
		return $this->_currentPage;
	}
	
	public function getTotalpage() {
		return $this->_totalPage;
	}

	public function __construct($pageTpl, $currentPage) {
		$this->_currentPage = intval($currentPage) < 1 ? 1 : intval($currentPage);
		$this->_pageTpl = $pageTpl;
	}
	
	public function __destruct(){
	
	}
	
}

